<?php
/**
 * Контроллер отвечает за парсинг
 * @author user
 */
class Controller_Parse extends Controller_Base {
	
	public function action() {
		if (!User_User::isAdmin()) throw new Exception_SiteError('', 403);
		
		switch ($this->arrRequest[1]) {
			case 'parse_lvl_country': $this->_actionParseLvlCountry(); break;
			case 'parse_lvl_city': $this->_actionParseLvlCity(); break;
			case 'parse_lvl_tours': $this->actionParseLvlTours(); break;
			case 'parse_weather': $this->actionParseWeather(); break;
			case 'update_price': $this->updatePrice(); break;
			default: throw new Exception_SiteError('', 404);
		}
	}
	
	/**
	 * Парсим страны level.travel
	 */
	private function _actionParseLvlCountry() {
		$objLvlApi = new Lvl_Api();
		$objLvlApi->parseCountries();
	}
	
	/**
	 * Парсим города level.travel
	 */
	private function _actionParseLvlCity() {
		$objLvlApi = new Lvl_Api();
		$objLvlApi->parseCities();
	}
	
	/**
	 * Парсим отели с турами
	 */
	private function actionParseLvlTours() {
		set_time_limit(0);
		$objLvlApi = new Lvl_Api();
		$objDB = Service_Database::instance();
		$arrCountries = $objDB->select('SELECT `iso`, `country_name_ru` FROM `lvl_country`');
		$objDateTime = new DateTime();
		$startDate = $objDateTime->modify('+1 week')->format('d.m.Y');
		
		foreach ($arrCountries as $arrCountry) {
			$objDB->query("DELETE FROM `lvl_tour` WHERE `iso` = '" . $arrCountry['iso'] . "'");
			$objLvlApi->parseTours(strtoupper($arrCountry['iso']), $startDate);
		}
	}
	
	private function actionParseWeather() {
		Weather::parseWeather();
	}
	
	private function updatePrice() {
		Country::updateMinPrices();
	}
}